using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace VoxelEngine
{
	public class LoadingScreenController : MonoBehaviour
	{
		[SerializeField]
		private CanvasGroup _canvasGroup;

		[SerializeField]
		private Image _progressImage;

		private Vector2 _currentScale = Vector2.one;

		private Vector2 _initialOffset = Vector2.one;

		private Vector2 _finalOffset = Vector2.one;

		private const float RotationSpeed = 60f;

		private void Awake()
		{
			_finalOffset = Vector2.zero;
			_initialOffset.x = 2f * _progressImage.rectTransform.rect.x;
			_initialOffset.y = 0f;
			_currentScale = _initialOffset;
			_currentScale.x = 0f;
			_progressImage.rectTransform.sizeDelta = _currentScale;
			UpdateProgress(0f);
		}

		public void UpdateProgress(float progress)
		{
			_progressImage.rectTransform.offsetMax = Vector2.Lerp(_initialOffset, _finalOffset, progress);
		}

		public IEnumerator Fadeout(float time, Action onFinish = null)
		{
			float currentTime = 0f;
			while (currentTime < time)
			{
				_canvasGroup.alpha = Mathf.Lerp(1f, 0f, currentTime / time);
				currentTime += Time.deltaTime;
				yield return null;
			}
			_canvasGroup.alpha = 0f;
			base.gameObject.SetActive(value: false);
			onFinish.Fire();
		}
	}
}
